package Tydzien3Test;
import static org.junit.Assert.*;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import Tydzien3.*;


public class Tydzien3Test {
	@Rule
	public ExpectedException exception = ExpectedException.none();
	
	@Test
	public void testAppendElement() {
		Tydzien3 instance = new Tydzien3();
		Ksiazka k = new Ksiazka("Biblia", "Nieznany", "Jakiestam", 2000);
		instance.appendElement(k);
		assertEquals( k, instance.getElement(0));
	}
	
	@Test
	public void testSize() {
		Tydzien3 instance = new Tydzien3();
		Ksiazka k = new Ksiazka("Biblia", "Nieznany", "Jakiestam", 2000);
		assertEquals( 0, instance.size());
		instance.appendElement(k);
		assertEquals( 1, instance.size());
	}
	
	@Test
	public void testArrayFlow() {
		Tydzien3 instance = new Tydzien3();
		Ksiazka k = new Ksiazka("Biblia", "Nieznany", "Jakiestam", 2000);
		for (int i = 0; i<11; i++) {
			instance.appendElement(k);
		}
		assertEquals(11, instance.size());
		assertEquals(k, instance.getElement(10));
	}
	
	@Test
	public void testRemoveElement() {
		Tydzien3 instance = new Tydzien3();
		Ksiazka k = new Ksiazka("Biblia", "Nieznany", "Jakiestam", 2000);
		instance.appendElement(new Ksiazka("Droga", "Justyna", "Alabama", 2005));
		instance.appendElement(k);
		exception.expect(ArrayIndexOutOfBoundsException.class);
		instance.removeElement(2);
		instance.removeElement(0);
		assertEquals( k, instance.getElement(0));
	}
	
	@Test
	public void testGetElement() {
		Tydzien3 instance = new Tydzien3();
		Ksiazka k = new Ksiazka("Biblia", "Nieznany", "Jakiestam", 2000);
		instance.appendElement(new Ksiazka("Droga", "Justyna", "Alabama", 2005));
		instance.appendElement(k);
		exception.expect(ArrayIndexOutOfBoundsException.class);
		instance.getElement(2);
	}
	
	@Test
	public void testSort() {
		Tydzien3 instance = new Tydzien3();
		instance.appendElement(new Ksiazka("Cena", "Zimny", "Jakiestam", 1998));
		instance.appendElement(new Ksiazka("Biblia", "Nieznany", "PWN", 2000));
		instance.appendElement(new Ksiazka("Droga", "Justyna", "Alabama", 2005));
		instance.sort();
		for (int i = 0; i < instance.size()-1; i++) {
			assertTrue((instance.getElement(i).toString()).compareTo(instance.getElement(i+1).toString()) < 0);
		}
	}
	
	@Test
	public void testSortTytul() {
		Tydzien3 instance = new Tydzien3();
		instance.appendElement(new Ksiazka("Cena", "Zimny", "Jakiestam", 1998));
		instance.appendElement(new Ksiazka("Biblia", "Nieznany", "PWN", 2000));
		instance.appendElement(new Ksiazka("Droga", "Justyna", "Alabama", 2005));
		Tydzien3 exact = new Tydzien3();
		exact.appendElement(new Ksiazka("Biblia", "Nieznany", "PWN", 2000));
		exact.appendElement(new Ksiazka("Cena", "Zimny", "Jakiestam", 1998));
		exact.appendElement(new Ksiazka("Droga", "Justyna", "Alabama", 2005));
		instance.sort(new KomparatorTytul());
		assertEquals(exact, instance);
		
	}
	@Test
	public void testSortAutor() {
		Tydzien3 instance = new Tydzien3();
		instance.appendElement(new Ksiazka("Cena", "Zimny", "Jakiestam", 1998));
		instance.appendElement(new Ksiazka("Biblia", "Nieznany", "PWN", 2000));
		instance.appendElement(new Ksiazka("Droga", "Justyna", "Alabama", 2005));
		Tydzien3 exact = new Tydzien3();
		exact.appendElement(new Ksiazka("Droga", "Justyna", "Alabama", 2005));
		exact.appendElement(new Ksiazka("Biblia", "Nieznany", "PWN", 2000));
		exact.appendElement(new Ksiazka("Cena", "Zimny", "Jakiestam", 1998));
		instance.sort(new KomparatorAutor());
		assertEquals(exact, instance);
		
	}

	@Test
	public void testSortRokWydania() {
		Tydzien3 instance = new Tydzien3();
		instance.appendElement(new Ksiazka("Droga", "Justyna", "Alabama", 2005));
		instance.appendElement(new Ksiazka("Cena", "Zimny", "Jakiestam", 1998));
		instance.appendElement(new Ksiazka("Biblia", "Nieznany", "PWN", 2000));
		Tydzien3 exact = new Tydzien3();
		exact.appendElement(new Ksiazka("Cena", "Zimny", "Jakiestam", 1998));
		exact.appendElement(new Ksiazka("Biblia", "Nieznany", "PWN", 2000));
		exact.appendElement(new Ksiazka("Droga", "Justyna", "Alabama", 2005));
		instance.sort(new KomparatorRokWydania());
		assertEquals(exact, instance);
		
	}	
	
	@Test
	public void testSortWydawnictwko() {
		Tydzien3 instance = new Tydzien3();
		instance.appendElement(new Ksiazka("Cena", "Zimny", "Jakiestam", 1998));
		instance.appendElement(new Ksiazka("Biblia", "Nieznany", "PWN", 2000));
		instance.appendElement(new Ksiazka("Droga", "Justyna", "Alabama", 2005));
		Tydzien3 exact = new Tydzien3();
		exact.appendElement(new Ksiazka("Droga", "Justyna", "Alabama", 2005));
		exact.appendElement(new Ksiazka("Cena", "Zimny", "Jakiestam", 1998));
		exact.appendElement(new Ksiazka("Biblia", "Nieznany", "PWN", 2000));
		instance.sort(new KomparatorWydawnictwo());
		assertEquals(exact, instance);
		
	}

}
